/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.ejbs;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import project.entity.Evento;
import project.entity.Submissao;

/**
 *
 * @author serafim
 */
@Stateless
public class SubmissaoFacade extends AbstractFacade<Submissao> {
    @PersistenceContext(unitName = "DAEPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public SubmissaoFacade() {
        super(Submissao.class);
    }
    
    public int countListaSubmissoes(String username) {
        Query cq = getEntityManager().createNamedQuery("listaSubmissoesAutor");
        cq.setParameter("username", username);
        return cq.getResultList().size();
    }
    
    public List<Submissao> getListaSubmissoes(int[] range, String username) {
        Query cq = getEntityManager().createNamedQuery("listaSubmissoesAutor");
        cq.setParameter("username", username);
        cq.setMaxResults(range[1] - range[0]);
        cq.setFirstResult(range[0]);
        return cq.getResultList();
    }

    public List<Submissao> getSubmissoesAutor(Evento evento, String username) {
        Query cq = getEntityManager().createNamedQuery("submissoesAutor");
        cq.setParameter("username", username);
        cq.setParameter("evento", evento);
        return cq.getResultList();
    }

    public boolean isUser(String email, long idSubmissao) {
        Query cq = getEntityManager().createNamedQuery("verificarAutor");
        cq.setParameter("email", email);
        cq.setParameter("idSubmissao", idSubmissao);
        return (Integer.parseInt(cq.getSingleResult().toString()) > 0);
    }
}
